package org.teamswift.crow.rbac.entity;

import com.google.common.base.Strings;
import lombok.Data;
import org.teamswift.crow.rbac.enums.CrowRowPermissionLevel;
import org.teamswift.crow.rbac.primaryKey.RoleForeignKey;
import org.teamswift.crow.rest.common.ICrowIO;

import javax.persistence.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

@Entity
@Data
@IdClass(RoleForeignKey.class)
@Table(
        uniqueConstraints = {
                @UniqueConstraint(
                        columnNames = {"roleId", "permissionResource", "action", "rowPermissionLevel"}
                )
        }
)
public class CrowAuthority implements ICrowIO {

    @Id
    @Column(nullable = false)
    private Integer roleId;

    @Id
    @Column(nullable = false, length = 100)
    private String permissionResource;

    @Id
    @Column(nullable = false, length = 50)
    private String action;

    @Transient
    private String shiroStyleAuthorization;

    @Transient
    private String uniqueKey;

    private CrowRowPermissionLevel rowPermissionLevel = CrowRowPermissionLevel.All;

    public String getShiroStyleAuthorization() {
        return String.format(
                "%s %s",
                action, permissionResource
        );
    }

    public String getUniqueKey() {
        return String.format(
                "%s-%s",
                permissionResource,
                rowPermissionLevel.name()
        );
    }
}
